var e = require("../../../@babel/runtime/helpers/objectSpread2"),
  a = i(require("../../../@babel/runtime/regenerator")),
  t = require("../../../@babel/runtime/helpers/asyncToGenerator"),
  r = i(require("../../../config/index")),
  n = require("../../../services/base/user"),
  s = require("../../../services/user-service"),
  o = require("@/services/base/user"),
  u = i(require("../../../config/page"));
function i(e) {
  return e && e.__esModule ? e : { default: e };
}
Page({
  data: { CDN: r.default.CDN, user: { name: "", phone: "", avatarUrl: "" } },
  onLoad: function () {
    var e = this;
    return t(
      a.default.mark(function t() {
        var r, s;
        return a.default.wrap(function (a) {
          for (;;)
            switch ((a.prev = a.next)) {
              case 0:
                return (a.next = 2), (0, n.getUser)();
              case 2:
                (r = a.sent),
                  console.log(r),
                  ((s = { name: "", phone: "", avatarUrl: "" }).name = r.data
                    .nick
                    ? r.data.nick
                    : ""),
                  (s.phone = r.data.phoneCipherText
                    ? r.data.phoneCipherText
                    : ""),
                  (s.avatarUrl = r.data.avatar ? r.data.avatar : ""),
                  e.setData({ user: s });
              case 9:
              case "end":
                return a.stop();
            }
        }, t);
      }),
    )();
  },
  changeAvatar: function (a) {
    var t = a.detail.avatarUrl,
      r = this.data.user;
    this.setData({ user: e(e({}, r), {}, { avatarUrl: t }) });
  },
  changeNick: function (e) {
    var a = e.detail.value,
      t = this.data.user;
    (t.name = a), this.setData({ user: t });
  },
  updateUserInfo: function () {
    var e = this;
    return t(
      a.default.mark(function t() {
        var r, n;
        return a.default.wrap(function (a) {
          for (;;)
            switch ((a.prev = a.next)) {
              case 0:
                return (
                  (r = e.data.user),
                  (n = { nick: r.name, avatar: r.avatarUrl }),
                  console.log(r),
                  (a.next = 5),
                  (0, s.updateUserInfo)(n)
                );
              case 5:
                wx.showModal({
                  content: "保存成功",
                  showCancel: !1,
                  success: function (e) {
                    e.confirm &&
                      wx.switchTab({ url: u.default.PAGE_MINE_INDEX });
                  },
                });
              case 6:
              case "end":
                return a.stop();
            }
        }, t);
      }),
    )();
  },
  getPhoneNumber: function (e) {
    var s = this;
    return t(
      a.default.mark(function t() {
        var u, i, c, l;
        return a.default.wrap(function (a) {
          for (;;)
            switch ((a.prev = a.next)) {
              case 0:
                if (
                  (console.log("点击传递事件", e),
                  "getPhoneNumber:ok" !== e.detail.errMsg)
                ) {
                  a.next = 19;
                  break;
                }
                return (
                  (i = e.detail.code),
                  (a.next = 5),
                  (0, o.postMobile)({ appId: r.default.appId, code: i })
                );
              case 5:
                if (
                  ((c = a.sent),
                  console.log("用户手机号接口返回：", c),
                  null == c ||
                    null === (u = c.data) ||
                    void 0 === u ||
                    !u.duplicateAuthorization)
                ) {
                  a.next = 10;
                  break;
                }
                return (
                  wx.showToast({
                    title: "该手机号已被用户授权",
                    icon: "none",
                    duration: 2500,
                  }),
                  a.abrupt("return")
                );
              case 10:
                if (c.data) {
                  a.next = 13;
                  break;
                }
                return (
                  wx.showToast({ title: c.msg, icon: "none", duration: 2500 }),
                  a.abrupt("return")
                );
              case 13:
                return (
                  wx.showModal({ content: "修改成功", showCancel: !1 }),
                  (a.next = 16),
                  (0, n.getUser)()
                );
              case 16:
                (l = a.sent),
                  s.setData({
                    "user.phone": l.data.phoneCipherText
                      ? l.data.phoneCipherText
                      : "",
                  }),
                  console.log("目前显示的用户手机号为：", s.data.user.phone);
              case 19:
              case "end":
                return a.stop();
            }
        }, t);
      }),
    )();
  },
});
